package epam.marchenko.library.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name = "users")
@NamedQueries({
	@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
	@NamedQuery(name = "User.countUsers", query = "SELECT count(u) FROM User u"),
	@NamedQuery(name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login=:login"),
	@NamedQuery(name = "User.searchByName", query = "SELECT u FROM User u WHERE u.name LIKE :name")
})
public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	int id;
	
	@Column(name="name", nullable = false)
	@Size(min = 3, max = 100, message = "Username must be between 3 and 100 characters long.")
	@Pattern(regexp = "^[a-zA-Z0-9 ']+$", message = "Username must be alphanumeric")
	String name;
	
	@Column(name="login", nullable = false)
	@Size(min = 3, max = 30, message = "Login must be between 3 and 30 characters long.")
	@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "Username must be alphanumeric without spaces")
	String login;
	
	@Column(name="password", nullable = false)
	@Size(min = 6, max = 30, message = "Password must be between 6 and 30 characters long.")
	String password;
	
	@Column(name="phone")
	@Size(min = 0, max = 100, message = "Phone must be between 0 and 100 characters long.")
	String phone;
	
	@Column(name="address")
	@Size(min = 0, max = 100, message = "Password must be between 0 and 100 characters long.")
	String address;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User "+id+") login="+login;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + id;
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (id != other.id)
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		return true;
	}

}
